﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Hosting;

namespace SX.Utils
{
    /// <summary>
    /// 服务端帮助
    /// </summary>
    public static partial class CommonHelper
    {

        /// <summary>
        /// 通过相对路径获取绝对路径
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string MapPath(string path)
        {
            return HostHelper.MapPath(path);
        }

        /// <summary>
        /// 获取相对Url
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string GetUrl(string url)
        {
            return UrlHelper.GetUrl(url);
        }

        /// <summary>
        ///  获取客户端IP
        /// </summary>
        /// <returns></returns>
        public static string GetClientIp()
        {
            if (HttpContext.Current == null || HttpContext.Current.Request == null)
            {
                return "0.0.0.0";
            }
            string result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

            if (null == result || result == String.Empty)
            {
                result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            }

            if (null == result || result == String.Empty)
            {
                result = HttpContext.Current.Request.UserHostAddress;
            } 
            return result;
        }

        /// <summary>
        /// 获取客户端信息
        /// </summary>
        /// <returns></returns>
        public static string GetUserAgent() {
            if (HttpContext.Current == null || HttpContext.Current.Request == null)
            {
                return "";
            }
            string result = HttpContext.Current.Request.UserAgent; 
            return result;
        }
    }
}
